﻿using Common;
using RentalDTO;
using RentalIService;
using RentalService.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace RentalService
{
    public class HouseService : IServiceHouse
    {
        /// <summary>
        /// 地址模糊查询自动补足
        /// </summary>
        /// <returns></returns>
        public List<string> ShowAddressList(string name)
        {
            using (var db = new RentalContext())
            {
                BaseService<House> house = new BaseService<House>(db);
                Expression<Func<House, bool>> model = u => u.IsDel == 1;
                var list = house.GetLists(model).Select(m => new HouseDTO
                {
                    Id = m.Id,
                    Address = m.Address
                }).ToList();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    list.Where(u => u.Address.Contains(name));
                }
                List<string> str = new List<string>();
                foreach (var item in list)
                {
                    str.Add(item.Address);
                }
                return str;
            }

        }
        /// <summary>
        /// 房源几室几厅下拉框
        /// </summary>
        /// <returns></returns>
        public List<HouseDTO> ShowDetailList()
        {
            using (var db = new RentalContext())
            {
                BaseService<House> house = new BaseService<House>(db);
                Expression<Func<House, bool>> model = u => u.IsDel == 1;
                var list = house.GetLists(model).OrderBy(u => u.detailed).Select(m => new HouseDTO
                {
                    Id = m.Id,
                    Detailed = m.detailed
                }).ToList();
                List<HouseDTO> detail = new List<HouseDTO>();
                foreach (var item in list)
                {
                    if (detail.Where(u => u.Detailed == item.Detailed).Count() == 0)
                    {
                        detail.Add(item);
                    }    
                }
                return detail;
            }

        }
        /// <summary>
        ///房源子级 查询租客信息
        /// </summary>
        /// <param name="houseId"></param>
        /// <returns></returns>
        public List<HouseRecordDTO> ShowChildTableList(string houseId)
        {
            using (var db = new RentalContext())
            {
                BaseService<HouseRecord> house = new BaseService<HouseRecord>(db);
                Expression<Func<HouseRecord, bool>> model = u => u.IsDel == 1;

                BaseService<landlordTenant> land = new BaseService<landlordTenant>(db);
                Expression<Func<landlordTenant, bool>> landname = u => u.IsDel == 1;
                var listName = land.GetLists(landname);
                var list = house.GetLists(model).Where(u => u.HouseId == houseId).Join(listName, m => m.landlordId, n => n.Id, (m, n) => new HouseRecordDTO
                {
                    Id = m.Id,
                    landlordName = n.Name,
                    CreateTime = m.CreateTime,
                   Tel=n.Tel,
                    EndTime = m.EndTime
                }).OrderBy(u => u.Id).ToList();
                return list;
            }

        }
        /// <summary>
        /// 房源管理页面查询展示数据
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="limit"></param>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <param name="detailed"></param>
        /// <param name="address"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public List<HouseDTO> ShowHouseList(int offset, int limit, string sort, string order, string detailed, string address, ref int rowCount)
        {
            using (var db = new RentalContext())
            {
                BaseService<House> house = new BaseService<House>(db);
                BaseService<landlordTenant> land = new BaseService<landlordTenant>(db);
                rowCount = 0;
                Expression<Func<landlordTenant, bool>> where = u => u.IsDel == 1;
                var query = Common.PredicateExtensions.True<House>();
                query = query.And<House>(u => u.IsDel == 1);
                if (!string.IsNullOrWhiteSpace(address))
                {
                    query = query.And<House>(u => u.Address.Contains(address));
                }
                if (!string.IsNullOrWhiteSpace(detailed) && detailed != "全部")
                {
                    query = query.And<House>(u => u.detailed == detailed);
                }
                var orderBy = Common.PredicateExtensions.GetSortLambda<House>(sort);
                bool orderType = order == "asc" ? true : false;
                var landList = land.GetLists(where);
                var list = house.GetPagedList(offset, limit, ref rowCount, query, orderBy, orderType).Join(landList, b => b.landlordId, m => m.Id, (b, m) => new HouseDTO
                {
                    Id = b.Id,
                    Address = b.Address,
                    Floore = b.floore,
                    LandlordName = m.Name,
                    Detailed = b.detailed,
                    HouseType = b.HouseType,
                    HouseMoney = b.HouseMoney,
                    WaterElectricity = b.WaterElectricity,
                    HouseLen = b.HouseLen,
                    PeopleCount = b.PeopleCount,
                    Remark = b.Remark,
                    IsFinal=b.IsFinal
                }).ToList();
                return list;
            }
        }
        /// <summary>
        /// 添加房源信息
        /// </summary>
        /// <param name="address"></param>
        /// <param name="Floore"></param>
        /// <param name="Detailed"></param>
        /// <param name="Type"></param>
        /// <param name="Remark"></param>
        /// <param name="HouseMoney"></param>
        /// <returns></returns>
        public bool AddHouse(string address, int HouseLen, string Floore, string Detailed, int Type, string Remark, int HouseMoney)
        {
            using (var db = new RentalContext())
            {
                BaseService<House> house = new BaseService<House>(db);
                House model = new House();
                model.Id = IdString.GetIdGuid();
                model.Address = address;
                model.floore = Floore;
                model.detailed = Detailed;
                model.HouseType = Type;
                model.Remark = Remark;
                model.HouseMoney = HouseMoney;
                model.IsAutit = 1;
                model.IsDel = 1;
                model.IsSubscribe = 1;
                model.TemplateId = "";
                model.IsFinal = 1;
                model.HouseLen = HouseLen;
                model.CreateTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;
                model.PeopleCount = int.Parse(Detailed.Substring(0, 1));
                model.landlordId = "t1";
                return house.Add(model);
            }
        }
        public bool UpdaHouse(string index, int HouseLen, string address, string Floore, string Detailed, int Type, string Remark, int HouseMoney)
        {
            using (var db = new RentalContext())
            {
                BaseService<House> list = new BaseService<House>(db);
                var model = db.House.Find(index);
                model.Address = address;
                model.floore = Floore;
                model.detailed = Detailed;
                model.HouseType = Type;
                model.Remark = Remark;
                model.HouseLen = HouseLen;
                model.HouseMoney = HouseMoney;
                model.UpdateTime = DateTime.Now;
                model.PeopleCount = int.Parse(Detailed.Substring(0, 1));
                return list.Update(model);
            }
        }

        public bool HouseDel(string lists)
        {
            using (var db = new RentalContext())
            {
                string[] listId = lists.Split(',');
                BaseService<House> model = new BaseService<House>(db);
                int m = 0;
                foreach (var item in listId)
                {
                    var delModel = db.House.Find(item);
                    delModel.IsDel = 0;
                    bool flag = model.Delete(delModel);
                    if (flag == false)
                    {
                        m += 1;
                    }
                }
                if (m <= 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public bool HouseXiaJia(string text, string id)
        {
            using (var db=new RentalContext())
            {
                BaseService<House> list = new BaseService<House>(db);
                var model = db.House.Find(id);
                model.ReturnRemark = text;
                model.IsFinal = 0;
                return list.Update(model);
            }
        }
    }
}
